package com.xbongbong.crm.perform.query.render.edit;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmEditPageRenderService;
import com.xbongbong.crm.help.FineClueHelp;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateGetVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.FineClueEnum;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CompetitorEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/9/15 17:12
 * @description:竞争对手 编辑 页面渲染
 */
@Slf4j
@Service("competitorEditPageRenderServiceImpl")
public class CompetitorEditPageRenderServiceImpl extends AbstractCrmEditPageRenderService {
    @Resource
    private FineClueHelp fineClueHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.COMPETITOR.getCode());
    }

    @Override
    public ParseSingleRowDataVO formatData(FormDataUpdateGetDTO formDataUpdateGetDTO, HandlerExplainVO handlerExplainVO, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        JSONObject data = paasFormDataEntityExt.getData();
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(handlerExplainVO.getHeadList());
        data.put(CompetitorEnum.VISBLE_DEPT.getAttr(), handlerExplainVO.getOwnerId());
        data.put(CompetitorEnum.VISBLE_USER.getAttr(), handlerExplainVO.getCoUserId());
        ParseSingleRowDataVO parseSingleRowDataVO = parseSingleRowData(formDataUpdateGetDTO, data, explainMap, handlerExplainVO.getUserMap(), handlerExplainVO.getDepartmentMap());
        paasFormDataEntityExt.setData(parseSingleRowDataVO.getResult());
        return parseSingleRowDataVO;
    }

    @Override
    public void businessHandleExplain(FormDataUpdateGetDTO formDataUpdateGetDTO, List<FieldAttrEntity> list, PaasFormEntityExt form, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        boolean formCopy = Objects.nonNull(formDataUpdateGetDTO.getSaasSpecialParamPojo()) && Objects.equals(formDataUpdateGetDTO.getSaasSpecialParamPojo().getFromCopy(), BasicConstant.ONE);
        handelSaasSpecialUpdate(list, formDataUpdateGetDTO.getBusinessType(), paasFormDataEntityExt, formCopy, formDataUpdateGetDTO.getDistributorMark());
    }

    @Override
    public void handelSaasSpecialUpdate(List<FieldAttrEntity> explainList, Integer businessType, PaasFormDataEntityExt formDataEntityExt, boolean formCopy, Integer distributorMark) throws XbbException {

    }

    @Override
    public void formatSaasAttrToReturn(FormDataUpdateGetDTO formDataUpdateGetDTO, FormDataUpdateGetVO formDataUpdateGetVO, PaasFormEntityExt form, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        formDataUpdateGetVO.setData(paasFormDataEntityExt);
        formDataUpdateGetVO.setPaasFormEntity(form);
        //精线索工商查询 通过pid控制icon显示
        String corpid = formDataUpdateGetDTO.getCorpid();
        JSONObject result = formDataUpdateGetVO.getData().getData();
        String companyName = result.getString(CompetitorEnum.NAME.getAttr());
        // 精线索
        String pid = fineClueHelp.getPidByCompanyName(corpid, companyName, formDataUpdateGetDTO.getPlatform());
        if (Objects.nonNull(pid)) {
            paasFormDataEntityExt.setPid(pid);
        }


        // 查看是否能使用精线索的工商查询
        boolean fineClueFlag = fineClueHelp.fineClueUsable(corpid, FineClueEnum.BUSINESS) && Objects.equals(DistributorMarkEnum.OTHER.getCode(), formDataUpdateGetDTO.getDistributorMark());
        if (fineClueFlag) {
            formDataUpdateGetVO.setIsFineClue(1);
        }
    }
}
